<?php
namespace app\index\controller;

class Reg extends \think\Controller
{
    public function index()
    {
        return view();
    }
    //普通注册
    public function insert()
    {
        $name = input('post.name','','strip_tags');
        $tell = input('post.tell','','strip_tags');
        $pwd = input('post.pwd','','strip_tags');
        $repwd = input('post.repwd','','strip_tags');
        $tell_code = input('post.tellcode','','strip_tags');
        $data = [
            'name'  => $name,
            'tell'  => $tell,
            'pwd'   => $pwd,
            'repwd' => $repwd,
        ];
        $validate = validate('Reg');       //加载验证器
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }
        if($tell_code != session('tell_code')['code'] || $tell != session('tell_code')['tell']){
            $this->error("短信验证码错误！");
        }
        unset($data['repwd']);
        $ret = db('user')->insert($data);
        if($ret == false){
            $this->error("注册失败！");
        }else{
            $this->success("注册成功！",'index/login/index');
        }
    }
    //短信验证手机号接口方法
    public function send()
    {
        $tell = input('get.tell','','htmlspecialchars');
        // 创蓝
        $api = "http://sms.quweiziyuan.cn/sms.php";
        $random = mt_rand(100000, 999999);
        $data = array(
            'key' => 'wein07699',
            'tell' => "{$tell}",
            'code' => "{$random}"
        );
        $row = ["tell"=>$tell,"code"=>$random];
        session('tell_code',$row);
        $ret = post($api, $data);
        echo $ret;
    }
}